var gridList;
$(document).ready(function () {
    // 取得并设置grid内容
    gridList = new dataGrid("gridList");
    gridList.load();
});

function formatOper(e) {
    var row = e.record;
    var activityId = row.hasOwnProperty('activityId') ? row.activityId : "";
    var sysTaskId = row.hasOwnProperty('sysTaskId') ? row.sysTaskId : "";
    var sysTaskKeyId = row.hasOwnProperty('sysTaskKeyId') ? row.sysTaskKeyId : "";
    var wfId = row.hasOwnProperty('wfId') ? row.wfId : "";
    var id = row.hasOwnProperty('id') ? row.id : "";
    return "<a class='layui-btn layui-btn-sm' hideFocus='' href='javascript:void(0)' onClick='getOptionType(\"" + wfId + "\",\"" + sysTaskId + "\",\"" + activityId + "\",\""+sysTaskKeyId+"\",\""+id+"\")'><i class='layui-icon'>&#xe653;</i>办理</a>";
}

function open_Detail(wfId, sysTaskId, activityId, sysTaskKeyId , id) {
    showProgressView(wfId, sysTaskKeyId, sysTaskId, activityId,id, 'viewTimeline.html', 'viewFlowchat.html', '../workFlow/workInstance.html');
}

/*
* 判断自己所在活动是否是多人抢占
* */
function getOptionType(wfId, sysTaskId, activityId , sysTaskKeyId , id) {
    ActHttpRequest("/wf/activity/" + activityId, {}, function (data) {
        if (data.OK == "OK") {
            if (data.wfActivity.operationType == "3") {
                ActHttpRequest("/wf/systask/key/getoptionty/" + sysTaskId, {}, function (data) {
                    if (data.OK == "OK") {
                        if (data.TYPE == "OK") {
                            open_Detail(wfId, sysTaskId, activityId, sysTaskKeyId , id);
                        }
                        else {
                            mini.alert("活动为抢占！已经有其他人审批!");
                        }
                    } else {
                        mini.alert(data.message);
                    }
                })
            } else {
                open_Detail(wfId, sysTaskId, activityId, sysTaskKeyId,id);
            }
        } else {
            mini.alert(data.message);
        }
    }, null, null, null, '加载中。。。', true);
}

function formatModuleId(e) {
    var row = e.record;
    var moduleName = "";
    ActHttpRequest("/wf/module/"+row.moduleId, {}, function (d) {
        moduleName = d.wfModule.moduleName;
    });
    return moduleName;
}

function formatActivityId(e) {
    var row = e.record;
    var activityName = "";
    ActHttpRequest("/wf/activity/"+row.activityId, {}, function (d) {
        activityName = d.wfActivity.activityName;
    });
    return activityName;
}

function formatUserId(e) {
    var row = e.record;
    var userName = "";

    $.ajax({
        type: "post",
        url: "/sys-user/"+row.userId,
        data: {},
        async: false,
        success: function(d){
            userName = d.sysUser.userName;
        }
    });

    return userName;
}

function formatSysTaskKeyState(e) {
    var row = e.record;
    var state = "";
    switch (row.sysTaskKeyState) {
        case 1:
            state = "办理中";
            break;
        case 2:
            state = "已办结";
            break;
        case 3:
            state = "已撤销";
            break;
    }
    return state;
}

function formatTitle(e) {
    var row = e.record;
    var record = JSON.parse(row.wfTaskRecord.replace(/\'/g,"\""));
    var wfTitle = row.wfTitle;
    var index_1 = wfTitle.indexOf("{{"),
        index_2 = wfTitle.indexOf("}}");
    var firstTitle = wfTitle.substring(0, index_1);
    var secondTitle = wfTitle.substring(index_2 + 2);
    var title = wfTitle.substring(index_1 + 2, index_2);
    var t = "";
    record.forEach(function (data) {
        if(data.name == title){
            t = data.values[0];
        }
    });
    t = firstTitle + t + secondTitle;
    return t;
}